﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DACU.Tools;
using DACU.VkontakteApi.Collections;

namespace DACU.UserControls
{
	/// <summary>
	/// Логика взаимодействия для FriendsList.xaml
	/// </summary>
	public partial class FriendsList : Window
	{
		GridViewColumnHeader _lastHeaderClicked = null;
		ListSortDirection _lastDirection = ListSortDirection.Ascending;

		public IEnumerable Source
		{
			get { return lvFrends.ItemsSource; }
			set { lvFrends.ItemsSource = value; }
		}

		public FriendsList()
		{
			InitializeComponent();
		}

		private void lvFrends_Click(object sender, RoutedEventArgs e)
		{
			var headerClicked = e.OriginalSource as GridViewColumnHeader;
			ListSortDirection direction;
			if (headerClicked != null)
			{
				if (headerClicked.Role != GridViewColumnHeaderRole.Padding)
				{
					if (headerClicked != _lastHeaderClicked)
						direction = ListSortDirection.Ascending;
					else
						direction = _lastDirection == ListSortDirection.Ascending
						            	? ListSortDirection.Descending
						            	: ListSortDirection.Ascending;
					var header = headerClicked.Column.Header as string;
					Sort(header, direction);

					//if (direction == ListSortDirection.Ascending)
					//    headerClicked.Column.Header="";
					//else
					//    headerClicked.Column.HeaderTemplate =
					//        Resources["HeaderTemplateArrowDown"] as DataTemplate;
					//if (_lastHeaderClicked != null && _lastHeaderClicked != headerClicked)
					//    _lastHeaderClicked.Column.HeaderTemplate = null;
					_lastHeaderClicked = headerClicked;
					_lastDirection = direction;
				}
			}
		}

		private void Sort(string header, ListSortDirection direction)
		{
			var friends = lvFrends.ItemsSource as VKFriendsCollection;
			if (friends == null || friends.IsEmpty) return;
			if (direction == ListSortDirection.Ascending)
				switch (header)
				{
					case "Is Online":
						lvFrends.ItemsSource = friends.OrderByDescending(friend => friend.IsOnline).ToFriends();
						break;
					case "First Name":
						lvFrends.ItemsSource = friends.OrderBy(friend => friend.FirstName).ToFriends();
						break;
					case "Last Name":
						lvFrends.ItemsSource = friends.OrderBy(friend => friend.LastName).ToFriends();
						break;
					case "Has Mobile":
						lvFrends.ItemsSource = friends.OrderBy(friend => friend.HasMobile).ToFriends();
						break;
				}
			else
				switch (header)
				{
					case "Is Online":
						lvFrends.ItemsSource = friends.OrderBy(friend => friend.IsOnline).ToFriends();
						break;
					case "First Name":
						lvFrends.ItemsSource = friends.OrderByDescending(friend => friend.FirstName).ToFriends();
						break;
					case "Last Name":
						lvFrends.ItemsSource = friends.OrderByDescending(friend => friend.LastName).ToFriends();
						break;
					case "Has Mobile":
						lvFrends.ItemsSource = friends.OrderByDescending(friend => friend.HasMobile).ToFriends();
						break;
				}
		}
	}
}
